package com.jereh.topnews.util;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.jereh.topnews.api.NewsApi;
import com.jereh.topnews.entity.Content;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

/**
 * http请求工具类
 * Created by Administrator on 2016/11/18.
 */
public class HttpUtil {

    /**
     * 通过api接口地址获取数据
     * @param apiUrl
     * @return
     */
    public static String getDataByApi(String apiUrl){
        BufferedReader reader = null;
        String result = null;
        StringBuffer sbf = new StringBuffer();
        try {
            URL url = new URL(apiUrl);
            HttpURLConnection connection = (HttpURLConnection) url
                    .openConnection();
            connection.setRequestMethod("GET");
            // 填入apikey到HTTP header
            connection.setRequestProperty("apikey", NewsApi.apikey);
            connection.connect();
            InputStream is = connection.getInputStream();
            reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
            String strRead = null;
            while ((strRead = reader.readLine()) != null) {
                sbf.append(strRead);
                sbf.append("\r\n");
            }
            reader.close();
            result = sbf.toString();
            sbf=null;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    /**
     * 通过json数据得到新闻列表
     * @param json
     * @return
     */
    public static List<Content> getContentByJson(String json){
        List<Content> list=new ArrayList<>();
        try {
            JSONObject object = JSON.parseObject(json);
            JSONObject body = (JSONObject) object.get("showapi_res_body");
            JSONObject pagebean = (JSONObject)body.get("pagebean");
            JSONArray contentlist=pagebean.getJSONArray("contentlist");
            list.addAll(JSON.parseArray(contentlist.toJSONString(), Content.class));
//            Content content=JSON.parseObject(contentlist.toJSONString(),Content.class);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return list;
    }

    /**
     * 通过url地址得到bitmap
     * @param imgUrl
     * @return
     */
    public static Bitmap getBitmapByUrl(String imgUrl){
        Bitmap bitmap=null;
        try {
            URL url=new URL(imgUrl);
            HttpURLConnection connection=(HttpURLConnection)url.openConnection();
            connection.setRequestMethod("GET");
            connection.setConnectTimeout(10000);
            connection.connect();
            InputStream is=connection.getInputStream();
            //通过bitmap工厂类提供的方法，将流转成bitmap图像资源
            bitmap= BitmapFactory.decodeStream(is);
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e){
            e.printStackTrace();
        }
        return bitmap;
    }

    /**
     * 解决整个工作流程的方法
     */
    public static void getDataWithCallback(){

    }
}
